<template>
	<view class="page">
		<navbar backIconName="arrow-left" title="我认证的企业" :titleStyle="titleStyle"></navbar>
		<view :style="{height:statusHeight+'px'}"></view>
		<scroll-view class="scroll-view_H"
			:style="{height: 'calc(100vh - ' + (statusHeight+(whereFrom == 'personal'?44:0)+safeAreaHeight)+'px)'}"
			scroll-y="true">
			<view v-if="dataList.length > 0" class="news-list">
				<view class="news-item" v-for="(item,index) in dataList" :key="index">
					<view class="staff-card">
						<view class="left">
							<view class="top">
								<view>
									<text
										style="font-size: 30rpx;color: #323232;font-weight: bold;">{{item.companyName || '-'}}
									</text>
									<text v-if="item.def === 0"
										style="font-size: 30rpx;color: #1169DB;font-weight: bold;margin-left: 4px;">(当前企业)</text>
								</view>

								<text
									style="font-size: 24rpx;color: #999;margin-top: 20rpx;">社会信用代码：{{item.socialCreditNumber || '-'}}
								</text>
							</view>
							<view class="bottom">
								<text class="status">已认证</text>
								<text style="font-size: 26rpx;color: #666;">{{item.createTime || '-'}}</text>
							</view>

						</view>
						<u-line v-if="item.def != 0 || (item.def == 0 && dataList.length == 1)"></u-line>
					</view>
					<view v-if="item.def != 0 || (item.def == 0 && dataList.length == 1)" class="menu-but">
						<view v-if="item.def != 0" class="set-default" @click="toDetails(item)">
							<text style="color: #1169DB;font-size: 30rpx;">设置默认企业</text>
						</view>
						<view v-if="item.def != 0 || dataList.length == 1" class="set-unbind" @click="toUnbind(item)">
							<text style="color: #E63C3C;font-size: 30rpx;">解绑企业</text>
						</view>
					</view>
				</view>
			</view>

			<view v-else class="content-empty">
				<u-empty mode="data" text="暂无认证的企业">
				</u-empty>
			</view>
		</scroll-view>
		<view v-if="whereFrom == 'personal'" style="height: 44px;margin: 0px 20rpx;">
			<u-button type="primary" text="认证新企业" @click="toNewCompany"></u-button>
		</view>

	</view>
</template>

<script>
	import {
		getAuthenticateList,
		setDefault,
		setUnbind,
	} from '@/api/pagesMine'
	import {
		showConfirm,
	} from "@/utils/common.js"
	let App = getApp();
	import store from '@/store'
	export default {
		components: {},
		data() {
			return {
				statusHeight: 44 + App.globalData.statusBarHeight,
				safeAreaHeight: App.globalData.safeAreaHeight,
				titleStyle: {
					color: "#ffffff",
					fontWeight: "bold",
					fontSize: "18px"
				},
				dataList: [],
				userId: store.state.user.USER_OPENID,
				whereFrom: "",
			};
		},
		onShow() {

		},
		onLoad(options) {
			this.whereFrom = options.whereFrom
			this.getDataList()
			this.$eventBus.$off('myAuthentication')
			this.$eventBus.$on('myAuthentication', (param) => {
				this.getDataList()
			})
		},
		onPullDownRefresh() {
			this.getDataList()
		},
		methods: {
			/* 获取数据 */
			getDataList() {
				let userId = store.state.user.USER_OPENID
				getAuthenticateList({
					userId: userId
				}).then(res => {
					this.dataList = res.data.data || []
					//网络请求获取后关闭下拉框
					uni.stopPullDownRefresh()
				});
			},
			/* 认证新企业 */
			toNewCompany() {
				uni.navigateTo({
					url: '/pages-mine/pages/newAuthenticationCompany/index'
				});
			},
			/* 设置为当前企业 */
			toDetails(row) {
				let userId = store.state.user.USER_OPENID
				showConfirm('确认要设置该企业为默认显示的企业?').then(res => {
					if (res.confirm) {
						setDefault({
							userId: userId,
							socialCreditNumber: row.socialCreditNumber
						}).then(res => {
							this.getDataList()
							this.$store.commit('user/CURRENT_COMPANY_INFO', res.data) //更新当前认证企业信息
							if (this.whereFrom == 'home' || this.whereFrom == 'myQrCode') {
								uni.navigateBack()
							}
						});
					}
				});
			},
			/* 解绑企业 */
			toUnbind(row) {
				let userId = store.state.user.USER_OPENID
				showConfirm('确认要解绑该企业?').then(res => {
					if (res.confirm) {
						setUnbind({
							userId: userId,
							socialCreditNumber: row.socialCreditNumber
						}).then(res => {
							if (this.dataList.length == 1) { //只剩一个企业
								this.$store.commit('user/CURRENT_COMPANY_INFO', null) //更新当前认证企业信息
								let userInfo = store.state.user.USER_INFO
								userInfo.isAuthentication = false
								userInfo.userCompanyRecords = []
								this.$store.commit('user/COMMIT_USER_INFO', userInfo) //更新当前认证企业信息
							}
							this.getDataList()

						});
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.content-empty {
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.page {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}


	.scroll-view_H {
		width: 100%;


	}

	.news-list {
		overflow-x: hidden;
		overflow-y: auto;

		.news-item {
			display: flex;
			flex-direction: column;
			background-color: #fff;
			margin: 10rpx 20rpx;
			border-radius: 12rpx;
			box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.15);

			.menu-but {
				display: flex;
				align-items: center;
				width: 100%;
				height: 50px;

				.set-default {
					flex: 1;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				.set-unbind {
					flex: 1;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}

			.staff-card {
				display: flex;
				flex-direction: column;
				padding: 20rpx 30rpx 0px 30rpx;

				.left {
					flex: 1;
					display: flex;
					flex-direction: column;
					padding-bottom: 30rpx;

					.top {
						display: flex;
						flex-direction: column;
						margin-bottom: 20rpx;
					}

					.bottom {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.status {
							font-size: 24rpx;
							color: #fff;
							padding: 4rpx 10rpx;
							border-radius: 6rpx;
							background-color: #36B900;
						}
					}
				}
			}
		}
	}
</style>